# syntax = docker/dockerfile:experimental
FROM ubuntu:18.04 as buildtemp 

WORKDIR /tmp
COPY . ./

FROM ubuntu:18.04

# 安装系统依赖
RUN echo "deb [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic main restricted universe multiverse\n\
    deb-src [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic main restricted universe multiverse\n\
    deb [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted universe multiverse\n\
    deb-src [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic-updates main restricted universe multiverse\n\
    deb [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse\n\
    deb-src [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic-backports main restricted universe multiverse\n\
    deb [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic-security main restricted universe multiverse\n\
    deb-src [trusted=yes] https://repo.huaweicloud.com/ubuntu/ bionic-security main restricted universe multiverse\n" > /etc/apt/sources.list && \
    apt-get -o "Acquire::https::Verify-Peer=false" update && \
    apt-get -o "Acquire::https::Verify-Peer=false" install -y --no-install-recommends pciutils wget libxml2 g++ automake make xz-utils libbz2-dev libssl-dev vim zip unzip dos2unix autoconf libtool \
    libgl1-mesa-glx zlib1g zlib1g-dev libffi-dev libfreetype6-dev pkg-config libpng-dev numactl libopenblas-dev bzip2 libblas3 liblapack3 \
    libblas-dev gfortran libhdf5-dev libicu60 libxml2-dev libxslt-dev ca-certificates curl gcc cython3 python3-h5py libgmpxx4ldbl less htop && \
    apt clean && rm -rf /var/lib/apt/lists/* && \
    if [ ! -d "/lib64" ]; \
    then \
        mkdir /lib64 && ln -sf /lib/ld-linux-aarch64.so.1 /lib64/ld-linux-aarch64.so.1; \
    fi
 
ENV LD_LIBRARY_PATH=/usr/local/python3.9.2/lib:$LD_LIBRARY_PATH
ENV PATH=/usr/local/python3.9.2/bin:$PATH

# 安装python和pip
RUN umask 0022 && \
    curl -k https://repo.huaweicloud.com/python/3.9.2/Python-3.9.2.tar.xz -o Python-3.9.2.tar.xz && \
    tar -xf Python-3.9.2.tar.xz && cd Python-3.9.2 && ./configure --prefix=/usr/local/python3.9.2 --enable-shared && \
    make -j9 && make install && \
    ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python3 && \
    ln -sf /usr/local/python3.9.2/bin/python3 /usr/bin/python && \
    ln -sf /usr/local/python3.9.2/bin/pip3 /usr/bin/pip3 && \
    ln -sf /usr/local/python3.9.2/bin/pip3 /usr/bin/pip && \
    cd .. && \
    rm -rf Python* && \
    mkdir -p ~/.pip  && \
    echo '[global] \n\
    index-url=http://pypi.douban.com/simple\n\
    trusted-host=pypi.douban.com' >> ~/.pip/pip.conf && \
    pip3 install pip -U && \
	
# 源码安装cmake
    apt autoremove cmake && \
    curl -O -k https://cmake.org/files/v3.20/cmake-3.20.5.tar.gz && \
    tar -zxvf cmake-3.20.5.tar.gz && \
    cd cmake-3.20.5 && \
    ./bootstrap && \
    make -j9 && \
    make install && \
    cd .. && \
    rm -rf cmake*

# HwHiAiUser
RUN groupadd  HwHiAiUser -g 1000 && \
    useradd -d /home/HwHiAiUser -u 1000 -g 1000 -m -s /bin/bash HwHiAiUser

RUN --mount=type=cache,target=/tmp,from=buildtemp,source=/tmp \
    umask 0022 && \
    pip3 install pip -U && \
    pip3 install -r /tmp/requirements.txt && \
    rm -rf /root/.cache/pip

# 环境变量设置
ARG ASCEND_BASE=/usr/local/Ascend
ENV ASCEND_HOME=$ASCEND_BASE
ARG MX_SDK_HOME=/home/HwHiAiUser/sdk_home/mxVision
ENV PYTHONPATH=${MX_SDK_HOME}/python:$ASCEND_BASE/ascend-toolkit/latest/pyACL/python/site-packages/acl:\
$ASCEND_BASE/ascend-toolkit/latest/atc/python/site-packages:$ASCEND_BASE/ascend-toolkit/latest/atc/python/site-packages/auto_tune.egg/auto_tune:\
$ASCEND_BASE/ascend-toolkit/latest/atc/python/site-packages/schedule_search.egg \
ASCEND_AICPU_PATH=$ASCEND_BASE/ascend-toolkit/latest \
MX_SDK_HOME=/home/HwHiAiUser/sdk_home/mxVision/ \
GST_PLUGIN_SCANNER=${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner \
GST_PLUGIN_PATH=${MX_SDK_HOME}/opensource/lib/gstreamer-1.0:${MX_SDK_HOME}/lib/plugins \
LD_LIBRARY_PATH=/home/data/miniD/driver/lib64:${MX_SDK_HOME}/lib/modelpostprocessors:${MX_SDK_HOME}/lib:${MX_SDK_HOME}/opensource/lib:${MX_SDK_HOME}/opensource/lib64:\
/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/driver:\
/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64:\
/usr/local/Ascend/ascend-toolkit/latest/atc/lib64:/usr/local/python3.9.2/lib

ENV PATH=$ASCEND_BASE/ascend-toolkit/latest/atc/ccec_compiler/bin:$ASCEND_BASE/ascend-toolkit/latest/atc/bin:$PATH

ENV ASCEND_OPP_PATH=$ASCEND_BASE/ascend-toolkit/latest/opp

# install toolkit pkg
RUN --mount=type=cache,target=/tmp,from=buildtemp,source=/tmp \
    umask 0022 && \
    chmod +x /tmp/Ascend-cann-toolkit*.run && \
    bash /tmp/Ascend-cann-toolkit*.run --quiet --install --install-path=/usr/local/Ascend/ --install-for-all && \
    mv /tmp/Ascend-mindxsdk*.run /home/HwHiAiUser && \
    chown HwHiAiUser:HwHiAiUser /home/HwHiAiUser/Ascend-mindxsdk*.run && \
    rm -rf /tmp/*

# 安装sdk包    
WORKDIR /home/HwHiAiUser
USER HwHiAiUser

RUN bash /home/HwHiAiUser/Ascend-mindxsdk*.run --quiet --install --install-path=/home/HwHiAiUser/sdk_home  && \
    rm /home/HwHiAiUser/Ascend-mindxsdk*.run

COPY --chown=HwHiAiUser:HwHiAiUser Open_Source_Software_Notice.txt /home/HwHiAiUser/Open_Source_Software_Notice.txt
COPY --chown=HwHiAiUser:HwHiAiUser ResNet152 /home/HwHiAiUser/ResNet152
COPY --chown=HwHiAiUser:HwHiAiUser test_model.sh /home/HwHiAiUser/test_model.sh